import cm.word.generate.pojo.mysql.MysqlTableDto;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import cm.word.generate.App;
import cm.word.generate.dao.mysql.MysqlQueryMapper;
import cm.word.generate.pojo.mysql.MysqlWordDto;
import cm.word.generate.utils.JacobOperaword;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.List;

/**
 * @author caomin
 * @date 2018/5/7
 * @说明 mysql生成
 */
@SpringBootTest(classes = App.class)
@RunWith(value = SpringRunner.class)
@SuppressWarnings("all")
public class MysqlTestWord {


    @Autowired
    MysqlQueryMapper mysqlQueryMapper;

    /**
     * tableIndex
     *
     * @throws Exception
     */
    @Test
    public void test() throws Exception {
        JacobOperaword jacobOperaword = new JacobOperaword(true);
        //创建文档
        jacobOperaword.createNewDocument();
        String dataBaseName = "njxt_zsb";
        String targetFilePath = "d:/njxt_zsb.doc";
        File file = new File(targetFilePath);
        if (file.getParentFile() == null) {
            file.mkdirs();
        }
        List<MysqlTableDto> tables = mysqlQueryMapper.getTables(dataBaseName);

        //所有表总览
        String tableList = "表清单";
        jacobOperaword.insertText(tableList);
        jacobOperaword.moveDown(2);
        jacobOperaword.moveRight(2);
        jacobOperaword.createTable(tableList, 3, tables.size() + 1);
        //设置表头
        jacobOperaword.putTxtToCell(1, 1, 1, "序号");
        jacobOperaword.putTxtToCell(1, 1, 2, "表名");
        jacobOperaword.putTxtToCell(1, 1, 3, "注释");
        for (Integer i = 0; i < tables.size(); i++) {
            jacobOperaword.putTxtToCell(1, i + 2, 1, i.toString());
            jacobOperaword.putTxtToCell(1, i + 2, 2, tables.get(i).getTableName());
            jacobOperaword.putTxtToCell(1, i + 2, 3, tables.get(i).getTableComment());
        }

        //下移两行,否则表会连在一起不能创建新的表格
        jacobOperaword.moveDown(2);
        jacobOperaword.moveRight(2);

        for (int i = 0; i < tables.size(); i++) {
            MysqlTableDto tableDto = tables.get(i);
            String tableComment = tableDto.getTableComment();
            String tableName = tableDto.getTableName();
            List<MysqlWordDto> wordList = mysqlQueryMapper.getColumnList(tableName);
            //TODO tableINdex是整个文档定位表格的参数
            int tableIndex = i + 2;

            //表格的行数及列数
            int cols = 5, rows = wordList.size() + 1;
            String text = null;
            if (!StringUtils.isEmpty(tableComment)) {
                text = tableComment + "(" + tableName + ")表详细设计";
            } else {
                text = tableName + "表详细设计";
            }
            jacobOperaword.insertText(text);
            jacobOperaword.moveDown(2);
            jacobOperaword.moveRight(2);
            jacobOperaword.createTable(text, cols, rows);
            //设置表头
            jacobOperaword.putTxtToCell(tableIndex, 1, 1, "代码");
            jacobOperaword.putTxtToCell(tableIndex, 1, 2, "名称");
            jacobOperaword.putTxtToCell(tableIndex, 1, 3, "数据类型");
            jacobOperaword.putTxtToCell(tableIndex, 1, 4, "是否为空");
            jacobOperaword.putTxtToCell(tableIndex, 1, 5, "主键");
            for (int j = 0; j < wordList.size(); j++) {
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 1, wordList.get(j).getColumnName());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 2, wordList.get(j).getBz());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 3, wordList.get(j).getAllColumnType());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 4, wordList.get(j).getSfwk());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 5, wordList.get(j).getColumnKey());
            }
            jacobOperaword.moveDown(3);
            jacobOperaword.insertText("我是华丽的分隔线");
        }
        jacobOperaword.save(targetFilePath);
        jacobOperaword.setSaveOnExit(true);
        jacobOperaword.closeDocumentWithoutSave();
        jacobOperaword.closeDocument();
        jacobOperaword.close();
    }

}



